from types import coroutine
import requests
import Config
from bs4 import BeautifulSoup
from entity.News import News
from lxml import etree


# ==获取新浪财经头条新闻==
def getSinaNews():
    newsList = []
    response = requests.get(Config.SINA_FINANCE_URL)

    response.encoding = "utf-8"

    soup = BeautifulSoup(response.text, "lxml")

    divContent = soup.find(id="blk_hdline_01")

    aContent = divContent.find_all("a")

    for item in aContent:
        if len(item.get_text().replace("\n", "")) < 7:  # 过滤掉无用数据（少于7个文字则无用）
            continue
        url = item.get("href")
        id =''
        if 'shtml' in url:
            id = url.split("-")[-1].split(".")[0]
        news = News('新浪财经', item.get_text().replace("\n", ""), item.get("href"), id)
        newsList.append(news.__dict__)

    url = Config.SINA_FINANCE_URL
    headers = {
        'User-Agent':
            'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
    }
    resp = requests.get(url=url, headers=headers)
    resp.encoding = 'utf-8'
    resp = resp.text
    tree = etree.HTML(resp)

    a_list = tree.xpath(
        '/html/body/div[@class="main"]/div[@class="m-part m-part1 udv-clearfix"]/div[@class="m-p-middle fleft"]//div[@class="fin_tabs0_c0"]//li//a')
    for i in a_list:
        title = i.xpath("./text()")[0]
        url = i.xpath("./@href")[0]
        if len(title.replace("\n", "")) < 7:  # 过滤掉无用数据（少于7个文字则无用）
            continue

        id =''
        if 'shtml' in url:
            id = url.split("-")[-1].split(".")[0]
        news = News('新浪财经', title.replace("\n", ""), url, id)
        newsList.append(news.__dict__)

    return newsList

